package base;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Assert;
import org.junit.Test;
import base.dao.UserDao;
import pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * @author Feuler
 * @since 2020/11/9
 **/

public class MybatisTest {

    final InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    public MybatisTest() throws IOException {
    }

    @Test
    public void test1() {
        try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
            final List<User> users = sqlSession.selectList("base.dao.UserDao.findAll");
            users.forEach(System.out::println);
            System.out.println("test 1 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test2() {
        try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
            User user = new User();
            user.setId(5);
            user.setUsername(Long.valueOf(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli()).toString());
            sqlSession.insert("base.dao.UserDao.saveUser", user);
            sqlSession.commit();
            sqlSession.selectList("base.dao.UserDao.findAll").forEach(System.out::println);
            System.out.println("test 2 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test3() {
        try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
            User user = new User();
            user.setId(5);
            user.setUsername(Long.valueOf(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli()).toString());
            sqlSession.update("base.dao.UserDao.updateUser", user);
            sqlSession.selectList("base.dao.UserDao.findAll").forEach(System.out::println);
            System.out.println("test 3 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test4() {
        try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
            sqlSession.delete("base.dao.UserDao.deleteUser", 5);
            sqlSession.selectList("base.dao.UserDao.findAll").forEach(System.out::println);
            System.out.println("test 4 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test5() {
        try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            userDao.findAll().forEach(System.out::println);
            System.out.println("test 5 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test6() {
        try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            User user = new User();
            user.setId(0);
            user.setUsername("张三");
            userDao.findByCondition(user).forEach(System.out::println);
            System.out.println("test 6 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test7() {
        try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            userDao.findByIds(new int[]{1, 2}).forEach(System.out::println);
            System.out.println("test 7 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

}
